<?php

namespace app\api\controller\v1;

use app\common\controller\BaseController;
use app\api\model\Course as CourseModel;
use app\api\model\TeacherCourse as TeacherCourseModel;
use app\api\model\TeacherCourseCollect as TeacherCourseCollectModel;
use think\Controller;
use think\Request;
use app\api\model\TeachingExperience as TeachingExperienceModel;
use app\api\model\Teacher as TeacherModel;
use app\api\model\TeacherBusy as TeacherBusyModel;

/**
 * 教师
 */

class Teacher extends BaseController
{
    protected $noNeedLogin = ['course', 'read', 'experience', 'busy'];
    /**
     * 首页课程
     */
    public function course()
    {
        $teacherCourse = TeacherCourseModel::getByRecommend();
        $teacherCourse ? $this->success('', ['teacher_couser' => $teacherCourse]) : $this->errorMsg(10001);
    }

    /**
     * 课程详情
     */
    public function read()
    {
        $teacherCourseId = $this->notEmpty('teacher_course_id');
        $teacherCourse = TeacherCourseModel::getByTeacherCourseId($teacherCourseId, true);
        $isCollect = false;
        if ($this->userId && TeacherCourseCollectModel::getCollect($teacherCourseId, $this->userId)) {
            $isCollect = true;
        }
        $teacherCourse['is_collect'] = $isCollect;
        $this->success('', ['teacher_couser' => $teacherCourse]);
    }

    /**
     * 教学经历
     */
    public function experience()
    {
        $teacherId = $this->notEmpty('teacher_id');
        $experience = TeachingExperienceModel::getByTeacherId($teacherId);
        $experience ? $this->success('', compact('experience')) : $this->notFoundData();
    }

    /**
     * 教师忙碌
     */
    public function busy()
    {
        $date = $this->notEmpty('date');
        $teacherCouseId = $this->notEmpty('teacher_course_id');
        $times =  TeacherBusyModel::busyTime($teacherCouseId, $date);
        $this->success('', compact('times'));
    }

    /**
     * 课程收藏
     */
    public function collect()
    {
        $teacherCourseId = $this->notEmpty('teacher_course_id');
        $userId = $this->userId;
        $collect = TeacherCourseCollectModel::getCollect($teacherCourseId, $userId);
        if ($collect) {
            $res = $collect->delete();
            $is_collect = false;
        } else {
            $res = TeacherCourseCollectModel::create(['teacher_course_id' => $teacherCourseId, 'user_id' => $userId]);
            $is_collect = false;
        }
        $res ? $this->success('', compact('is_collect')) : $this->forbidden();
    }
}
